﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using Raize.CodeSiteLogging;
using Pub;

namespace LZQXT.Test.Forms
{
    public partial class FOptRain : Form
    {
        ADOTest ado1;

        public FOptRain()
        {
            InitializeComponent();
        }

        private void FOptRain_Load(object sender, EventArgs e)
        {
            init();
        }

        private void init()
        {
            try
            {
                ado1 = Program.fdb_tree.ado1;

                string strSQL = "SELECT * FROM P_Product_AppField WHERE F_Name = 'rain'";
                DataSet ds1 = ado1.sel1(strSQL);

                listBox1.DataSource = ds1.Tables[0];
                listBox1.DisplayMember = "Oth";
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
            }

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.Text == string.Empty) return;

            refStaInfo(listBox1.Text);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveStaID();
        }

        private void refStaInfo(string P_Name)
        {
            try
            {
                string strSQL = "SELECT * FROM P_Product_AppField WHERE F_Name = 'rain' AND Oth like '" + P_Name + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                string P_ID = ds1.Tables[0].Rows[0]["P_ID"].ToString();

                /*
                string strSQL2 = "SELECT A.*, B.StaID FROM ( "
                    + "SELECT A.StationName AS 县区,B.StationName AS 站点,B.StationID AS 站号 FROM P_RainStation_Config A "
                    + "RIGHT JOIN P_RainStation_Config B ON A.ID = B.ParentID AND A.ParentID=-1 WHERE B.StationID <> '' "
                    + ") A LEFT JOIN P_Rain_Grid B ON A.站号=B.StaID AND B.P_ID='" + P_ID + "'";
                DataSet ds2 = ado1.sel1(strSQL);
                */
                treeView1.Nodes.Clear();
                //填充县区名称
                string strSQL2 = "SELECT * FROM P_RainStation_Config WHERE ParentID=-1";
                DataSet ds2 = ado1.sel1(strSQL2);
                for (int i = 0; i < ds2.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds2.Tables[0].Rows[i];
                    string sXQ = dr1["StationName"].ToString();
                    string ID = dr1["ID"].ToString();
                    TreeNode node1 = new TreeNode();
                    node1.Text = sXQ;
                    node1.Tag = "";

                    //填充站点名称
                    string strSQL3 = "SELECT A.*,B.StaID FROM P_RainStation_Config A LEFT JOIN P_Rain_Grid B ON A.StationID=B.StaID "
                        + " AND B.P_ID=" + P_ID + " WHERE A.ParentID=" + ID;
                    DataSet ds3 = ado1.sel1(strSQL3);
                    for (int j = 0; j < ds3.Tables[0].Rows.Count; j++)
                    {
                        DataRow dr3 = ds3.Tables[0].Rows[j];
                        string sStaName = dr3["StationName"].ToString();
                        string sStaID = dr3["StationID"].ToString();
                        string sChk = dr3["StaID"].ToString();
                        TreeNode node2 = new TreeNode();
                        node2.Text = sStaName;
                        node2.Tag = sStaID;
                        if (sChk != string.Empty) node2.Checked = true;

                        node1.Nodes.Add(node2);
                    }

                    node1.Expand();
                    treeView1.Nodes.Add(node1);
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("雨情站点查询" + ex);
            }

        }

        private void saveStaID()
        {
            try
            {
                string strSQL = "SELECT * FROM P_Product_AppField WHERE F_Name = 'rain' AND Oth like '" + listBox1.Text + "'";
                DataSet ds1 = ado1.sel1(strSQL);
                string P_ID = ds1.Tables[0].Rows[0]["P_ID"].ToString();

                foreach (TreeNode nodeXQ in treeView1.Nodes)
                {
                    foreach (TreeNode node1 in nodeXQ.Nodes)
                    {
                        string staID = "";
                        string staName = "";

                        if (node1.Tag.ToString() != string.Empty)
                        {
                            staID = node1.Tag.ToString();
                            staName = node1.Text;

                            string strSQL2 = "SELECT * FROM P_Rain_Grid WHERE P_ID=" + P_ID + " AND StaID='" + staID + "'";
                            DataSet ds2 = ado1.sel1(strSQL2);
                            if (CDB.dsIsNull(ds2) && node1.Checked == true)
                            {
                                string strSQL3 = "INSERT INTO P_Rain_Grid (ID,P_ID,Tab_ID,StaID,Oth) VALUES("
                                    + CDB.getNewID("P_Rain_Grid", "ID") + "," + P_ID + ",0,'" + staID + "','" + staName + "')";
                                CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);
                            }
                            else if (!CDB.dsIsNull(ds2) && node1.Checked == false)
                            {
                                string strSQL3 = "DELETE FROM P_Rain_Grid WHERE P_ID=" + P_ID + " AND StaID='" + staID + "'";
                                CDebug.Send(strSQL3);
                                ado1.cmd1(strSQL3);
                            }
                        }
                    }
                }
            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("雨情站点维护" + ex);
            }

        }

    }
}
